package actions;

import java.util.Map;

import org.apache.struts2.components.ActionError;
import org.apache.struts2.dispatcher.SessionMap;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dao.DataAccessObject;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {

	private String username;
	private String password;
	SessionMap<String, String> sessionmap;
	DataAccessObject dao = new DataAccessObject();

	public LoginAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		boolean result = dao.checkLogin(username, password);
		String sResult = null;
		if (result) {
			Map session = ActionContext.getContext().getSession();
			session.put("USER", username);
			sResult = "success";
		}else {
			addActionError("Invalid username/password.");
			sResult = "error";
		}

		return sResult;
	}

	public String logout() throws Exception {
		Map session = ActionContext.getContext().getSession();
		session.remove("USER");
		session.remove("context");
		return SUCCESS;
	}

}
